import React, { useEffect, useState } from 'react';
import { useSelector, useDispatch } from 'react-redux'
import { getArticleList } from "../../store/counterSlice";
import instance from "../../apis/request";
import { Button, Table ,Modal } from "antd";
import Search from "../../compued/Search";
import { getTian } from "../../apis/apis";
import Articleadd from "../../compued/Articleadd";
function Detail() {
    const {articleData,count} = useSelector(state => state.counter)
  const dispatch = useDispatch()
  const [data,setData]=useState({
    page:1,//页码  必传
pageSize:10,//每页显示条数 必传
keyword:null,//搜索标题
tag:null//搜索标签

  })
  const [fy,setFy]=useState({
    current:1,
    pageSize:10,
    total:0
  })
  const aa=async()=>{
    const res=await instance.get('/article/list',{
        params:data
    }).then(res=>{
        dispatch(getArticleList(res))
        setFy({
            ...fy,
            total:res.count
        })
    })
  }
  useEffect(()=>{
    aa()
  },[data,dispatch])
  const dd=(diss)=>{
    setData({
        ...data,
        page:diss.current,
        pageSize:diss.pageSize
    })
    setFy({
        ...fy,
        current:diss.current,
        pageSize:diss.pageSize,
        total:diss.count
    })
  }
  const columns = [
  {
    title: '消息id',
    dataIndex: 'id',
    key: 'id',
  },
  {
    title: '消息名称',
    dataIndex: 'title',
    key: 'title',
  },
  {
    title: '消息描述词',
    dataIndex: 'content',
    key: 'content',
  },
  {
    title: '创建时间',
    dataIndex: 'createdAt',
    key: 'createdAt',
  },
//   {
//     title: '创建人',
//     dataIndex: 'content',
//     key: 'content',
//   },
];
const onchangeSou=(k)=>{
    setData({
        ...data,
        keyword:k.keyword
    })
}

const [isModalOpen, setIsModalOpen] = useState(false);

  const showModal = async() => {
    setIsModalOpen(true);
    
  };

  const handleOk = () => {
    setIsModalOpen(false);
  };

  const handleCancel = () => {
    setIsModalOpen(false);
  };
    return (
        <div>
            <Search kkk={onchangeSou} />
            <Button type='primary' onClick={showModal}>添加消息</Button>
            <Table dataSource={articleData} columns={columns} pagination={fy} onChange={dd} />
            <Modal
        title="Basic Modal"
        closable={{ 'aria-label': 'Custom Close Button' }}
        open={isModalOpen}
        onOk={handleOk}
        onCancel={handleCancel}
      >
        <Articleadd handleCancel={handleCancel} setData={setData}/>
      </Modal>
        </div>
    );
}

export default Detail;